<template>
  <el-dialog
    :title="!dataForm.commentId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="评论内容" prop="commentTitle">
      <el-input v-model="dataForm.commentTitle" placeholder="评论内容"></el-input>
    </el-form-item>
    <el-form-item label="评论人id" prop="customerId">
      <el-input v-model="dataForm.customerId" placeholder="评论人id"></el-input>
    </el-form-item>
    <el-form-item label="楼市圈id" prop="commentSpaceId">
      <el-input v-model="dataForm.commentSpaceId" placeholder="楼市圈id"></el-input>
    </el-form-item>
    <el-form-item label="评论时间" prop="commentCredate">
      <el-input v-model="dataForm.commentCredate" placeholder="评论时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          commentId: 0,
          commentTitle: '',
          customerId: '',
          commentSpaceId: '',
          commentCredate: ''
        },
        dataRule: {
          commentTitle: [
            { required: true, message: '评论内容不能为空', trigger: 'blur' }
          ],
          customerId: [
            { required: true, message: '评论人id不能为空', trigger: 'blur' }
          ],
          commentSpaceId: [
            { required: true, message: '楼市圈id不能为空', trigger: 'blur' }
          ],
          commentCredate: [
            { required: true, message: '评论时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.commentId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.commentId) {
            this.$http({
              url: this.$http.adornUrl(`/proprietary/commenthome/info/${this.dataForm.commentId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.commentTitle = data.commentHome.commentTitle
                this.dataForm.customerId = data.commentHome.customerId
                this.dataForm.commentSpaceId = data.commentHome.commentSpaceId
                this.dataForm.commentCredate = data.commentHome.commentCredate
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/proprietary/commenthome/${!this.dataForm.commentId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'commentId': this.dataForm.commentId || undefined,
                'commentTitle': this.dataForm.commentTitle,
                'customerId': this.dataForm.customerId,
                'commentSpaceId': this.dataForm.commentSpaceId,
                'commentCredate': this.dataForm.commentCredate
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
